package com.example.xianji.utils.sp;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * 作者：张玉辉 on 2018/3/30 10:43.
 */

public class PreferenceEngine {
    private Application appContext;

    private PreferenceEngine(){
    }

    public static PreferenceEngine getInstance(){
        return PreferenceEngineHolder.instance;
    }

    private static class PreferenceEngineHolder {
        private static final PreferenceEngine instance = new PreferenceEngine();
    }


    public void init(Application appContext){
        this.appContext = appContext;
    }

    private SharedPreferences getDefaultSharedPreference(String name){
        return appContext.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public boolean containKey(String preferenceName, String key){
        return getDefaultSharedPreference(preferenceName).contains(key);
    }

    public String getString(String preferenceName, String key,String defaultString){
        String s = getDefaultSharedPreference(preferenceName).getString(key,defaultString);
        return s;
    }

    public void putString(String preferenceName, String key, String stringValue){
        SharedPreferences sharedPreferences = getDefaultSharedPreference(preferenceName);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key,stringValue);
        editor.commit();
    }

    public int getInt(String preferenceName,String key, int defaultInt){
        int i = getDefaultSharedPreference(preferenceName).getInt(key,defaultInt);
        return i;
    }

    public void putInt(String preferenceName, String key, int intValue) {
        SharedPreferences preference = getDefaultSharedPreference(preferenceName);
        SharedPreferences.Editor editor = preference.edit();
        editor.putInt(key, intValue);
        editor.commit();
    }

    public boolean getBoolean(String preferenceName, String key,
                              boolean defaultBoolean) {
        boolean b = getDefaultSharedPreference(preferenceName).getBoolean(key,
                defaultBoolean);
        return b;
    }

    public void putBoolean(String preferenceName, String key, boolean booleanValue) {
        SharedPreferences preference = getDefaultSharedPreference(preferenceName);
        SharedPreferences.Editor editor = preference.edit();
        editor.putBoolean(key, booleanValue);
        editor.commit();
    }

    public long getLong(String preferenceName, String key, long defaultLong) {
        long i = getDefaultSharedPreference(preferenceName).getLong(key, defaultLong);
        return i;
    }

    public void putLong(String preferenceName, String key, long longValue) {
        SharedPreferences.Editor editor = getDefaultSharedPreference(
                preferenceName).edit();
        editor.putLong(key, longValue);
        editor.commit();
    }

    public void remove(String preferenceName,String key){
        SharedPreferences.Editor editor = getDefaultSharedPreference(preferenceName).edit();
        editor.remove(key);
        editor.commit();
    }

    public void clear(String preferenceName) {
        SharedPreferences preference = getDefaultSharedPreference(preferenceName);
        preference.edit().clear().commit();
    }
}
